using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Net.Sockets;

namespace Microsoft.Samples.BizTalk.Adapter.Tcp.Send
{
    /// <summary>
    ///     This class represents a work item used to submit unordered batches to BizTalk.
    /// </summary>
    /// <author>
    ///     Paul Brown
    /// </author>
    public class BatchWorkItem : IWorkItem
    {
        #region Private Instance Members

        private WorkItemStatusTypes _status;            // The status of the work item
        private SendLocation        _sendLocation;      // The send location involved in the operation
        private List<BatchMessage>  _batchMessages;     // The batch messages

        #endregion

        #region Constructor

        /// <summary>
        ///     This constructor initializes the class.
        /// </summary>
        public BatchWorkItem(SendLocation sendLocation)
        {
            // Validate arguments
            if (sendLocation == null) throw new ArgumentNullException("sendLocation");

            // Initialize members
            this._sendLocation = sendLocation;
            this._status = WorkItemStatusTypes.Ready;
            this._batchMessages = new List<BatchMessage>();
        }

        #endregion

        #region Public Instance Properties

        /// <summary>
        ///     This property returns the send location that produced this work item.
        /// </summary>
        public SendLocation SendLocation
        {
            get { return (this._sendLocation); }
        }

        /// <summary>
        ///     This property returns the batch to be processed.
        /// </summary>
        public List<BatchMessage> BatchMessages
        {
            get { return (this._batchMessages); }
        }

        #endregion

        #region IWorkItem Members

        #region Public Instance Properties

        /// <summary>
        ///     This property gets or sets the status of the work item.
        /// </summary>
        public WorkItemStatusTypes Status
        {
            get { return (this._status); }
            set { this._status = value; }
        }

        #endregion

        #region Public Instance Methods

        /// <summary>
        ///     This method saves the content of the work item to the folder location specified by the adapter.
        /// </summary>
        public void Save()
        {
            // Check status
            switch (this.Status)
            {
                case WorkItemStatusTypes.Ready :
                case WorkItemStatusTypes.InProgress:

                    // Only save if a message exists in batch messages
                    foreach (BatchMessage message in this.BatchMessages)
                    {
                        if (message.Message != null)
                        {
                            // There is a BizTalk message - save the stream data
                            if (message.Message.BodyPart != null)
                            {
                                Stream stream = message.Message.BodyPart.GetOriginalDataStream();

                                if (stream != null)
                                {
                                    stream.Position = 0;
                                    DeadMessage.WriteStreamToDisk(this.SendLocation.Adapter, stream);
                                }
                            }
                        }
                    }

                    break;

                case WorkItemStatusTypes.Complete :

                    // Do nothing
                    break;
            }
        }

        #endregion

        #endregion
    }
}
